home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2001 September / PC-WELT 9-2001.ISO / software / hw / brennen / flask_src.exe / Misc / SelectorDialog.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-08  |  7.3 KB  |  256 lines

  1. /* 
  2.  *  SelectorDialog.cpp
  3.  *
  4.  *    Copyright (C) Alberto Vigata - January 2000
  5.  *
  6.  *  This file is part of FlasKMPEG, a free MPEG to MPEG/AVI converter
  7.  *    
  8.  *  FlasKMPEG is free software; you can redistribute it and/or modify
  9.  *  it under the terms of the GNU General Public License as published by
  10.  *  the Free Software Foundation; either version 2, or (at your option)
  11.  *  any later version.
  12.  *   
  13.  *  FlasKMPEG is distributed in the hope that it will be useful,
  14.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  *  GNU General Public License for more details.
  17.  *   
  18.  *  You should have received a copy of the GNU General Public License
  19.  *  along with GNU Make; see the file COPYING.  If not, write to
  20.  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
  21.  *
  22.  */
  23.  
  24. #include <windows.h>
  25. #include "..\resource.h"
  26. #include "..\auxiliary.h"
  27.  
  28. #include "selectordialog.h"
  29. //global
  30. TSelectorDialog   *set;
  31. CArr <int>        listbox_mirror;
  32.  
  33.  
  34. /* Rendering selections */
  35. void RenderSectionSelection(HWND hDlg, int section)
  36. {
  37.     int i;  
  38.     for(i=0; i<set->strings[section].GetCount(); i++)
  39.     {
  40.         if( set->selected[section][i] )
  41.             SendDlgItemMessage( hDlg, IDC_SELECTOR, LB_SELITEMRANGE, true,MAKELPARAM(set->first_in_section[section]+i,set->first_in_section[section]+i ));
  42.     }
  43. }
  44. void RenderSelection( HWND hDlg )
  45. {
  46.     int i;
  47.     // First, unselect all items
  48.     int list_items = SendDlgItemMessage( hDlg, IDC_SELECTOR, LB_GETCOUNT   , 0     , 0);
  49.     for( i=0; i<list_items; i++)
  50.         SendDlgItemMessage( hDlg, IDC_SELECTOR, LB_SELITEMRANGE, false, MAKELPARAM(i,i) );
  51.     // Now, render selections
  52.     for(i=0; i<set->section_count; i++)
  53.         RenderSectionSelection(hDlg, i);
  54. }
  55. void MirrorList(HWND hDlg, CArr<int> &array)
  56. {
  57.  
  58.     int placed,i;
  59.  
  60.     int list_items = SendDlgItemMessage( hDlg, IDC_SELECTOR, LB_GETCOUNT   , 0     , 0);
  61.     int    *selected  = new int[list_items];
  62.     placed         = SendDlgItemMessage( hDlg, IDC_SELECTOR, LB_GETSELITEMS, list_items, (LPARAM)selected);
  63.  
  64.  
  65.     //reset current array
  66.     array.SetArraySize(list_items);
  67.     for(i=0; i<list_items; i++)
  68.          array[i] = 0;
  69.     // mark selected items
  70.     for(i=0; i<placed; i++)
  71.          array[ selected[i] ] = 1;
  72.     delete [] selected;
  73. }
  74.  
  75.  
  76. /* Section handling */
  77. int  GetSection(HWND hDlg, int listbox_item)
  78. {
  79.     int i;
  80.     CArr<int>      current_selected;
  81.     MirrorList(hDlg, current_selected);
  82.  
  83.     for(i=0; i<set->section_count; i++)
  84.     {
  85.         if(listbox_item>= set->first_in_section[i]   &&
  86.            listbox_item<= set->last_in_section[i])
  87.            return i;
  88.     }
  89.     return 0;
  90. }
  91.  
  92. int  GetSectionPos(HWND hDlg, int listbox_item)
  93. {
  94.     int i;
  95.     CArr<int>      current_selected;
  96.     MirrorList(hDlg, current_selected);
  97.  
  98.     for(i=0; i<set->section_count; i++)
  99.     {
  100.         if(listbox_item>= set->first_in_section[i]   &&
  101.            listbox_item<= set->last_in_section[i])
  102.            return listbox_item - set->first_in_section[i];
  103.     }
  104.     return 0;
  105. }
  106. int GetSectionSelCount(HWND hDlg, int section)
  107. {
  108.     int selected=0,i;
  109.     for(i=0; i<set->strings[section].GetCount(); i++)
  110.     {
  111.         if(set->selected[section][i])
  112.             selected++;
  113.     }
  114.     return selected;
  115. }
  116. bool ItemIsInSection(HWND hDlg, int item)
  117. {
  118.     int i;
  119.     CArr<int>      current_selected;
  120.     MirrorList(hDlg, current_selected);
  121.  
  122.     for(i=0; i<set->section_count; i++)
  123.     {
  124.         if(item>= set->first_in_section[i]   &&
  125.            item<= set->last_in_section[i])
  126.            return true;
  127.     }
  128.     return false;
  129. }
  130. int  FindFirstDiff(CArr<int> &first, CArr<int> &second)
  131. {
  132.     int count,i;
  133.     if( first.GetCount() != second.GetCount() )
  134.         return 0;
  135.     count = first.GetCount();
  136.     for(i=0; i<count; i++)
  137.         if(first[i]!=second[i])
  138.             break;
  139.     return i;
  140. }
  141.  
  142.  
  143. LRESULT CALLBACK DlgSelector(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  144. {
  145.     unsigned int i,j, clicked_listitem, section, section_pos;
  146.     CArr<int>  current_selected;
  147.  
  148.     switch (message)
  149.     {
  150.         case WM_INITDIALOG:    
  151.              SetText(hDlg, set->tittle);
  152.              DlgSetText(hDlg, R_RIGHT_SELECTOR_DIALOG, set->lateral_text);
  153.              DlgSetText(hDlg, IDOK, set->button_text );
  154.  
  155.              // Build the presentation list
  156.             for(i=0; i<set->section_count; i++){
  157.                    SendDlgItemMessage(hDlg, IDC_SELECTOR, LB_ADDSTRING, 0, (LPARAM)set->sections_titles[i]);
  158.                  set->first_in_section[i] = 
  159.                      1 + SendDlgItemMessage(hDlg, IDC_SELECTOR, LB_ADDSTRING, 0, (LPARAM)"------------------------------------------------------------");
  160.  
  161.                  for(j=0; j<set->strings[i].GetCount(); j++){
  162.                     set->last_in_section[i]=SendDlgItemMessage(hDlg, IDC_SELECTOR, LB_ADDSTRING, 0, (LPARAM)set->strings[i][j]);
  163.                  }
  164.                  SendDlgItemMessage(hDlg, IDC_SELECTOR, LB_ADDSTRING, 0, (LPARAM)"");
  165.                  //Select streams
  166.             }
  167.             RenderSelection(hDlg);
  168.             // Now get a mirror image of the list box
  169.             MirrorList(hDlg, listbox_mirror);
  170.             return TRUE;
  171.  
  172.         case WM_COMMAND:
  173.             if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 
  174.             {
  175.                 EndDialog(hDlg, LOWORD(wParam));
  176.                 return TRUE;
  177.             }
  178.             
  179.  
  180.             switch (LOWORD(wParam)) 
  181.             { 
  182.                 case IDC_SELECTOR: 
  183.                     switch (HIWORD(wParam)) 
  184.                     { 
  185.                       case LBN_SELCHANGE: 
  186.                         // Guess if the item clicked belonged to a section
  187.                         MirrorList(hDlg, current_selected);
  188.                         clicked_listitem = FindFirstDiff(listbox_mirror, current_selected);
  189.                         if( ItemIsInSection(hDlg, clicked_listitem) )
  190.                         {
  191.                            // The item belongs to a section.
  192.                            // Guess section
  193.                            section      =    GetSection( hDlg, clicked_listitem );
  194.                            section_pos  = GetSectionPos( hDlg, clicked_listitem );
  195.  
  196.                            // if SINGLE_SELECT
  197.                            if( set->section_mode[section]&SINGLE_SELECT )
  198.                            {
  199.                                int clicked_state = set->selected[section][section_pos];
  200.                                 // Unselect all items in section
  201.                                 for(i=0; i<set->strings[section].GetCount(); i++)
  202.                                    set->selected[section][i] = 0;
  203.                                     // select clicked item
  204.                                 set->selected[section][section_pos] = !clicked_state;
  205.                            }
  206.                            if( set->section_mode[section]&MULTIPLE_SELECT )
  207.                            {
  208.                                 set->selected[section][section_pos] = !set->selected[section][section_pos];
  209.                            }
  210.                            if( set->section_mode[section]&MUST_SELECT )
  211.                            {
  212.                                 // if there are no selected items in this section
  213.                                 if (GetSectionSelCount(hDlg, section) == 0)
  214.                                     set->selected[section][section_pos] = 1;
  215.                            }
  216.                              
  217.                         }
  218.                         RenderSelection(hDlg);
  219.                         // Now get a mirror image of the list box
  220.                         MirrorList(hDlg, listbox_mirror);
  221.                         return TRUE; 
  222.                     
  223.                    } 
  224.                    break; 
  225.  
  226.                   // Destroy the dialog box. 
  227.  
  228.                   EndDialog(hDlg, TRUE); 
  229.                   return TRUE; 
  230.  
  231.                 default: 
  232.                   return FALSE; 
  233.             } 
  234.  
  235.             break;
  236.     }
  237.     return FALSE;
  238. }
  239.  
  240. void ResetSelections(TSelectorDialog *set){
  241.     int i,j;
  242.     for(i=0; i<set->section_count; i++)
  243.         set->selected[i].SetArraySize( set->strings[i].GetCount() );
  244.  
  245.     for(i=0; i<set->section_count; i++)
  246.     {
  247.         for(j=0; j<set->strings[i].GetCount(); j++)
  248.             set->selected[i][j]=0;
  249.     }
  250. }
  251.  
  252. int OpenSelectorDialog(HWND hWnd, HINSTANCE hInstance, TSelectorDialog *settings){
  253.     set = settings;
  254.     return DialogBox(hInstance, (LPCTSTR)(IDD_STRSELECTOR), hWnd ,(DLGPROC)DlgSelector);
  255. }
  256.